package com.panshi.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.panshi.entity.AppUserT;
import com.panshi.result.R;
import com.panshi.service.impl.AppUserTServiceImpl;
import com.panshi.util.RedisUtil;
import com.panshi.util.UserLoginUtils;
import com.panshi.util.UserRegMailUtils;
import com.panshi.util.UserRegSendSmsUtils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 网站用户表 前端控制器
 * </p>
 *
 * @author xhlMr
 * @since 2020-08-04
 */
@RestController
@ApiModel("网站用户表 前端控制器")
public class AppUserTController {

    @Autowired
    private AppUserTServiceImpl appUserTService;

    @Autowired
    private UserLoginUtils userLoginUtils;

    @Autowired
    private UserRegMailUtils userRegMailUtils;

    @Autowired
    private UserRegSendSmsUtils userRegSendSmsUtils;

    @Autowired
    private RedisUtil redisUtil;

    @GetMapping("/user/memberStop/{id}/{type}")
    public Boolean memberStop(@PathVariable int id,@PathVariable String type){
        AppUserT au = new AppUserT();
        au.setUserId(id);
        au.setUserStatus(type);
        return appUserTService.updateById(au);
    }


    @ApiOperation("用户修改 按ID")
    @PostMapping("user/upd")
    public R userUpdate(@RequestBody AppUserT appUserT){
        boolean b = appUserTService.updateById(appUserT);
        R r = new R();
        if(b){
            r.setCode(201);
            r.setSuccess(true);
            r.setMessage("修改成功");
            return r;
        }else{
            r.setCode(301);
            r.setSuccess(false);
            r.setMessage("修改失败");
        }
        return R.error();
    }



    @ApiOperation("按用户名查询  加分页")
    @GetMapping("/user/getUserByName/{userNickname}/{page}/{limit}")
    public List<AppUserT> getUserLikeName(@PathVariable String userNickname,@PathVariable int page,@PathVariable int limit){
        if(page<1){
            page=1;
        }
        // 查询这个分类下的所有问题，获取查询的数据信息
        Page<AppUserT> pageParam = new Page<>(page, limit);
        appUserTService.page(pageParam,new QueryWrapper<AppUserT>().like("user_nickname", userNickname));

        List<AppUserT> userPageAll = pageParam.getRecords();
        return userPageAll;
    }

    @ApiOperation("按分页查询全部")
    @GetMapping("/user/getUserPageAll/{page}/{limit}")
    public List<AppUserT> getUserPageAll(@PathVariable int page,@PathVariable int limit){
        if(page<1){
            page=1;
        }
        // 查询这个分类下的所有问题，获取查询的数据信息
        Page<AppUserT> pageParam = new Page<>(page, limit);
        appUserTService.page(pageParam,new QueryWrapper<AppUserT>());

        List<AppUserT> userPageAll = pageParam.getRecords();
        return userPageAll;
    }


    @ApiOperation(" 用户 找回密码")
    @GetMapping("/findpwd/{username}/{userpassword}/{code}/{type}")
    public R findpwd(@PathVariable String username,@PathVariable String userpassword,@PathVariable String code,@PathVariable String type){
        R r = new R();
        Object o = redisUtil.get(username+"_3");
        //判断前台的验证码与redis的验证码是否正确
        if(o != null){
            if(!o.equals(code)){
                r.setCode(301);
                r.setSuccess(false);
                r.setMessage("验证码已经过期");
                return r;
            }
        }else{
            r.setCode(301);
            r.setSuccess(false);
            r.setMessage("请输入正确的正确验证码");
            return r;
        }

        if(type ==null){
            return R.error();
        }
        AppUserT appUserT = new AppUserT();
        if(type.equals("1")){
        //    邮箱修改密码
            appUserT.setUserPassword(userpassword);
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("user_email",username);
            boolean update = appUserTService.update(appUserT, wrapper);
            if(update){
                redisUtil.setRemove(username+"_3");
                r.setCode(201);
                r.setSuccess(true);
                r.setMessage("修改成功");
                return r;
            }
        }else if(type.equals("2")){
        //    手机号修改密码
            appUserT.setUserPassword(userpassword);
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("user_telephone",username);
            boolean update = appUserTService.update(appUserT, wrapper);
            if(update){
                redisUtil.setRemove(username+"_3");
                r.setCode(201);
                r.setSuccess(true);
                r.setMessage("修改成功");
                return r;
            }
        }
        return R.error();
    }




    @ApiOperation("用户 登录")
    @PostMapping("/app-user-t/login")
    public R login(@RequestBody AppUserT appUserT){
        String type = appUserT.getUserStatus();     // 0 账号密码登录
        String username = appUserT.getUserEmail();  //用户账号
        String userPassword = appUserT.getUserPassword();   //用户密码
        R r = new R();

        if(type.equals("0")){

            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("user_telephone",username);
            wrapper.eq("user_password",userPassword);
            AppUserT one = appUserTService.getOne(wrapper); //手机账号密码登录
            if(one!=null){
                r.setCode(200);
                r.setSuccess(true);
                r.setMessage("手机号登录成功");
                r.setData(one.getUserId());
                return r;
            }
            QueryWrapper wrapper1 = new QueryWrapper();
            wrapper1.eq("user_email",username);
            wrapper1.eq("user_password",userPassword);
            AppUserT one1 = appUserTService.getOne(wrapper1);//邮箱账号密码登录
            if(one1!=null){
                r.setCode(200);
                r.setSuccess(true);
                r.setMessage("邮箱登录成功");
                r.setData(one1.getUserId());
                return r;
            }

            r.setCode(301);
            r.setSuccess(false);
            r.setMessage("账号密码错误");

            return r;
        }else if(type.equals("1")){
            System.out.println("短信登录判断账号短信"+type);
            Object o = redisUtil.get(username + "_1");
            if(o==null){
                r.setCode(301);
                r.setSuccess(false);
                r.setMessage("请输入正确验证码");
                return r;
            }

            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("user_telephone",username);
            AppUserT one = appUserTService.getOne(wrapper); //手机账号密码登录
            if(one!=null){
                r.setCode(200);
                r.setSuccess(true);
                r.setMessage("验证码登录成功");
                r.setData(one.getUserId());
                redisUtil.setRemove(username + "_1");
                return r;
            }
            QueryWrapper wrapper1 = new QueryWrapper();
            wrapper1.eq("user_email",username);
            AppUserT one1 = appUserTService.getOne(wrapper1);//邮箱账号密码登录
            if(one1!=null){
                r.setCode(200);
                r.setSuccess(true);
                r.setMessage("验证码登录成功");
                r.setData(one1.getUserId());
                redisUtil.setRemove(username + "_1");
                return r;
            }
            r.setCode(301);
            r.setSuccess(false);
            r.setMessage("请输入正确的账号");
        }
        System.out.println("服务异常-------------》");
        return R.error();
    }



    @ApiOperation("验证前台用户  是否有效 有效重新返回用户对象")
    @GetMapping("/app-user-t/veruser/{userBank}/{userPassword}")
    public AppUserT getVeruser(@PathVariable String userBank,@PathVariable String userPassword){
        String userEmail = userBank;
        int result1 = userBank.indexOf("@");
        AppUserT userphone = null;
        if(result1 != -1) {
            userphone = userLoginUtils.useremail(userEmail,userPassword);
        }else{
            userphone = userLoginUtils.userphone(userEmail,userPassword);
        }

        System.out.println(userBank+" "+userphone.toString());

        if(userphone != null){
            return userphone;
        }
        return userphone;
    }



    @ApiOperation("用户注册")
    @GetMapping("/app-user-t/add/{userName}/{code}/{userPassword}/{type}")
    public R userAdd(@PathVariable String userName,@PathVariable String code,@PathVariable String userPassword,@PathVariable String type){
        System.out.println(userName+" "+code+" "+userPassword+" "+type);
        R r = new R();
        Object o = redisUtil.get(userName+"_2");
        //判断前台的验证码与redis的验证码是否正确
        if(o != null){
            if(!o.equals(code)){
                r.setCode(301);
                r.setSuccess(false);
                r.setMessage("验证码已经过期");
                return r;
            }
        }else{
            r.setCode(301);
            r.setSuccess(false);
            r.setMessage("请输入正确的正确验证码");
            return r;
        }

        if(type.equals("1")){
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("user_email",userName);
            AppUserT one = appUserTService.getOne(wrapper);

            if(one!=null){
                r.setCode(301);
                r.setSuccess(false);
                r.setMessage("该账号已被绑定");
                return r;
            }else{

                boolean save = appUserTService.save(new AppUserT().setUserEmail(userName).setUserPassword(userPassword));
                if(save){
                    redisUtil.setRemove(userName+"_2");
                    r.setCode(201);
                    r.setSuccess(save);
                    r.setMessage("注册成功");
                    return r;
                }
            }

        }else if(type.equals("2")){
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("user_telephone",userName);
            AppUserT one = appUserTService.getOne(wrapper);
            if(one!=null){
                r.setCode(301);
                r.setSuccess(false);
                r.setMessage("该账号已被绑定");
                return r;
            }else{
                boolean save = appUserTService.save(new AppUserT().setUserTelephone(userName).setUserPassword(userPassword));
                if(save){
                    redisUtil.setRemove(userName+"_2");
                    r.setCode(201);
                    r.setSuccess(save);
                    r.setMessage("注册成功");
                    return r;
                }
            }
        }
        return R.error();
    }




    @ApiOperation("用户获取验证码")
    @GetMapping("/app-user-t/reg/{type}/{username}/{form}")
    public R reg(@PathVariable String type,@PathVariable String username,@PathVariable String form){
        System.out.println("用户获取验证码 ---------------------》");
        if(username!=null){
            if(type.equals("1")){
                return userRegMailUtils.regMail(username,form); //邮箱验证码
            }else if (type.equals("2")){
                return userRegSendSmsUtils.regSendSms(username,form);    //手机号验证码
            }
        }
        R r = new R();
        r.setCode(301);
        r.setSuccess(false);
        r.setMessage("系统繁忙");
        return r;
    }



    @ApiOperation("用户表 查询全部")
    @GetMapping("/app-user-t/all")
    public List<AppUserT> all(){
        return appUserTService.list(null);
    }

    @ApiOperation("用户表 按id查询")
    @GetMapping("/app-user-t/byid/{id}")
    public AppUserT byid(@PathVariable("id") Integer id){
        return appUserTService.getById(id);
    }

    @ApiOperation("用户表 添加元素")
    @PostMapping("/app-user-t/add")
    public Boolean add(@Validated AppUserT appUserT){
        return appUserTService.save(appUserT);
    }

    @ApiOperation("用户表 按id删除")
    @GetMapping("/app-user-t/del/{id}")
    public Boolean del(@PathVariable("id") Integer id){
        AppUserT au = new AppUserT();
        au.setUserId(id);
        au.setUserStatus("0");
        return appUserTService.updateById(au);
    }

    @ApiOperation("用户表 修改")
    @PostMapping("/app-user-t/upd")
    public Boolean upd(@Validated AppUserT appUserT){
        return appUserTService.updateById(appUserT);
    }


    @ApiOperation("用户手机号修改")
    @GetMapping("/app-user-t/userPhoneUpd/{userName}/{code}/{userId}")
    public R userPhoneUpd(@PathVariable String userName,@PathVariable String code,@PathVariable int userId){

        System.out.println(userName+" "+code+" "+userId+" ");
        R r = new R();
        Object o = redisUtil.get(userName+"_4");
        //判断前台的验证码与redis的验证码是否正确
        if(o != null){
            if(!o.equals(code)){
                r.setCode(301);
                r.setSuccess(false);
                r.setMessage("验证码已经过期");
                return r;
            }
        }else{
            r.setCode(301);
            r.setSuccess(false);
            r.setMessage("请输入正确的正确验证码");
            return r;
        }

        AppUserT user_telephone = appUserTService.getOne(new QueryWrapper<AppUserT>().eq("user_telephone", userName));
        if(user_telephone!=null && user_telephone.getUserId()!=null){
            r.setCode(301);
            r.setSuccess(false);
            r.setMessage("该账户已被绑定");
            return r;
        }
        //验证码没问题修改
        AppUserT appUserT = new AppUserT();
        appUserT.setUserTelephone(userName);
        appUserT.setUserId(userId);
        boolean b = appUserTService.updateById(appUserT);
        if(b){
            redisUtil.setRemove(userName+"_4");
            r.setCode(201);
            r.setSuccess(b);
            r.setMessage("修改成功");
            return r;
        }
        return R.error();
    }



    @ApiOperation("用户邮箱号修改")
    @GetMapping("/app-user-t/userEmailUpd/{userName}/{code}/{userId}")
    public R userEmailUpd(@PathVariable String userName,@PathVariable String code,@PathVariable int userId){
        System.out.println(userName+" "+code+" "+userId+" ");
        R r = new R();
        Object o = redisUtil.get(userName+"_4");
        //判断前台的验证码与redis的验证码是否正确
        if(o != null){
            if(!o.equals(code)){
                r.setCode(301);
                r.setSuccess(false);
                r.setMessage("验证码已经过期");
                return r;
            }
        }else{
            r.setCode(301);
            r.setSuccess(false);
            r.setMessage("请输入正确的正确验证码");
            return r;
        }

        AppUserT user_telephone = appUserTService.getOne(new QueryWrapper<AppUserT>().eq("user_email", userName));
        if(user_telephone!=null && user_telephone.getUserId()!=null){
            r.setCode(301);
            r.setSuccess(false);
            r.setMessage("该账户已被绑定");
            return r;
        }
        //验证码没问题修改
        AppUserT appUserT = new AppUserT();
        appUserT.setUserEmail(userName);
        appUserT.setUserId(userId);
        boolean b = appUserTService.updateById(appUserT);
        if(b){
            redisUtil.setRemove(userName+"_4");
            r.setCode(201);
            r.setSuccess(b);
            r.setMessage("修改成功");
            return r;
        }
        return R.error();
    }

    @ApiOperation("查看用户密码是否一致")
    @PostMapping("/app-user-t/getUserPassWord")
    public AppUserT getUserPassWord(@RequestBody AppUserT appUserT){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("user_id",appUserT.getUserId());
        wrapper.eq("user_password",appUserT.getUserPassword());
        return appUserTService.getOne(wrapper);
    }


    @ApiOperation("修改密码")
    @PostMapping("/app-user-t/userUpdatePwd")
    public Boolean userUpdatePwd(@RequestBody AppUserT appUserT){
        return appUserTService.updateById(appUserT);
    }

    @ApiOperation("修改用户图片")
    @PostMapping("/app-user-t/userImgUpload")
    public Boolean userImgUpload(@RequestBody AppUserT appUserT){
        return appUserTService.updateById(appUserT);
    }

}

